package de.koller.worlders.engine.entity.zone;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.math.Vec;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.engine.entity.IRenderable;
import de.koller.worlders.engine.entity.ITileEntity;
import de.koller.worlders.engine.entity.mo.MovingObject;
import de.koller.worlders.engine.tiles.ITile;
import de.koller.worlders.engine.tiles.Tile;
import de.koller.worlders.engine.util.WLightWeightNode;
import java.util.HashSet;
import java.util.Set;

public class Zone extends WLightWeightNode implements ITileEntity, ITile {

	private Tile tile;
	private Set<IZoneEffect> zoneEffect = new HashSet<>();
	private boolean inits = false;
	
	public Zone() {
		super();
	}

	@Override
	public void generate( WorldersRoot root, Object... references ) {
		super.generate( root, references );
		
		if( references.length < 1 || references[0] instanceof Tile == false )
			throw new OceanException( "first argument must be the TileObject!" );
		tile = (Tile)references[0];
		
		for( IZoneEffect e : zoneEffect )
			e.init( this );
		
		inits = true;
	}

	@Override
	public void ungenerate() {
		super.ungenerate();

		for( IZoneEffect e : zoneEffect )
			e.release();
	}
	
	public void enter( MovingObject mo ) {
		for( IZoneEffect e : zoneEffect )
			e.enter( mo );
	}
	
	public void in( MovingObject mo ) {
		for( IZoneEffect e : zoneEffect )
			e.in( mo );
	}
	
	public void leave( MovingObject mo ) {
		for( IZoneEffect e : zoneEffect )
			e.leave( mo );
	}

	public Zone addEffect( IZoneEffect ze ) {
		zoneEffect.add( ze );
		if( inits )
			ze.init( this );
		return this;
	}

	@Override
	public Tile getTile() {
		return tile;
	}

	@Override
	public Vec getPosition() {
		return tile.getPosition();
	}

	@Override
	public Vec getSize() {
		return tile.getSize();
	}

	public Set<IZoneEffect> getZoneEffect() {
		return zoneEffect;
	}

	@Override
	public IRenderable getVisual() {
		return tile.getVisual();
	}

	@Override
	public float getZ() {
		throw new UnsupportedOperationException( "Not supported yet." );
	}

}
